package hms.reservations;

import java.sql.*;
import java.util.Arrays;
import java.util.Iterator;

import hms.database.*;
import hms.rooms.Room;
import hms.rooms.RoomTable;

/*This class sets all interactions with Reservation table in DB
 * 
 * @author Edgar Onukwugha*/

public class Reservation extends DBRowObject{

	private int paying_customer_ID;
	private Boolean booked_in_hotel;
	private Boolean is_child;
	private Date check_in_date; 
	private Date check_out_date;
	private Boolean is_checked_in;
	private double reservation_fee;
	private int rewards_points_earned;
	
	public Reservation(){
		super();
		paying_customer_ID = -1;
		booked_in_hotel = false;
		is_child = false;
		check_in_date = null;
		check_out_date = null;
		is_checked_in = false;
		reservation_fee = 0.00;
		rewards_points_earned = -1;
	}
	
	public Reservation(int reservation_ID){
		super(reservation_ID);
	}
	
	/////////////////////////////////////////////////
	/////               SETTERS                //////
	/////////////////////////////////////////////////
		
	public void setPayingCustomerID(int i){
		paying_customer_ID = i;
		markAsChanged();
	}
	
	public void setBookedInHotel(Boolean i){
		booked_in_hotel = i;
		markAsChanged();
	}
	
	public void setIsChild(Boolean i){
		is_child = i;
		markAsChanged();
	}
	
	public void setCheckInDate(Date i){
		check_in_date = i;
		markAsChanged();
	}
	
	public void setCheckOutDate(Date i){
		check_out_date = i;
		markAsChanged();
	}
	
	public void setIsCheckedIn(Boolean i){
		is_checked_in = i;
		markAsChanged();
	}
	
	public void setReservationFee(double i){
		reservation_fee = i;
		markAsChanged();
	}	
	
	public void setRewardsPointsEarned(int i){
		rewards_points_earned = i;
		markAsChanged();
	}
	
	/////////////////////////////////////////////////
	/////               GETTERS                //////
	/////////////////////////////////////////////////
	
	public int getPayingCustomerID(){
		return paying_customer_ID;
	}
	
	public Boolean getBookedInHotel(){
		return booked_in_hotel;
	}
	
	public Boolean getIsChild(){
		return is_child;
	}
	
	public Date getCheckInDate(){
		return check_in_date;
	}
	
	public Date getCheckOutDate(){
		return check_out_date;
	}
	
	public Boolean getIsCheckedIn(){
		return is_checked_in;
	}
	
	public double getReservationFee(){
		return reservation_fee;
	}	
	
	public int getRewardsPointsEarned(){
		return rewards_points_earned;
	}

	// DB Access methods
	public RoomTable loadAssociatedRooms() {

		// get empty room list
		RoomTable roomList = new RoomTable();
		roomList.clear();
		if (this.isInDatabase()) {
			DAO localDAO = DAOnoGUI.getDAO();
			// retrieve data from ROOM_RESERVATION table
			ResultSet rs = localDAO.getResult("SELECT ROOMID FROM ROOM_RESERVATION WHERE RESERVATION_ID="+this.getUniqueIdentifier()+";");
			try {
				while(rs.next()) {
					roomList.add(new Room(rs.getInt("ROOMID")));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DAOnoGUI.getDAO();
		}
		return roomList;
	}
	
	@Override
	protected String[] getFieldNames() {
		String[] output = new String[8];
		output[0] = "PAYING_CUSTOMER_ID";
		output[1] = "BOOKED_IN_HOTEL";
		output[2] = "IS_CHILD";
		output[3] = "CHECK_IN";
		output[4] = "CHECK_OUT";
		output[5] = "IS_CHECKED_IN";
		output[6] = "RESERVATION_FEE";
		output[7] = "REWARDS_POINTS_EARNED";
		
		return output;
	}

	@Override
	protected void loadPreparedStatement(PreparedStatement stmt)
			throws SQLException {
		stmt.setInt(1, paying_customer_ID);
		stmt.setBoolean(2, booked_in_hotel);
		stmt.setBoolean(3, is_child);
		stmt.setDate(4, check_in_date);
		stmt.setDate(5, check_out_date);
		stmt.setBoolean(6, is_checked_in);
		stmt.setDouble(7, reservation_fee);
		stmt.setInt(8, rewards_points_earned);
	}

	@Override
	protected String getUniqueIdentifierFieldName() {
		// TODO Auto-generated method stub
		return "RESERVATION_ID";
	}

	@Override
	protected String getTableName() {
		// TODO Auto-generated method stub
		return "RESERVATION";
	}

	@Override
	protected void loadFromDatabase(ResultSet rs) throws SQLException {
		Iterator<String> i = Arrays.asList(getFieldNames()).iterator();
		paying_customer_ID = rs.getInt(i.next());
		booked_in_hotel = rs.getBoolean(i.next());
		is_child = rs.getBoolean(i.next());
		check_in_date = rs.getDate(i.next());
		check_out_date = rs.getDate(i.next());
		is_checked_in = rs.getBoolean(i.next());
		reservation_fee = rs.getDouble(i.next());
		rewards_points_earned = rs.getInt(i.next());
	}

	@Override
	protected boolean allowDuplicateRows() {
		// TODO Auto-generated method stub
		return false;
	}

}
